package com.rc.saas.tenant.service.log;

import com.rc.saas.tenant.common.utils.DateHelper;
import com.rc.saas.tenant.common.vo.log.ChargeProcessLogVo;
import com.rc.saas.tenant.mapper.elec.ElecGunMapper;
import com.rc.saas.tenant.mapper.elec.ElecStationMapper;
import com.rc.saas.tenant.mapper.member.MemberInfoMapper;
import com.rc.saas.tenant.mapper.order.OrderInfoMapper;
import com.rc.saas.tenant.model.elec.ElecGun;
import com.rc.saas.tenant.model.elec.ElecStation;
import com.rc.saas.tenant.model.member.MemberInfo;
import com.rc.saas.tenant.model.order.OrderInfo;
import com.rc.saas.tenant.model.order.OrderInfoExample;
import com.rc.saas.tenant.mongo.ChargeProcessLogDao;
import com.rc.saas.tenant.vo.log.ChargedProcessLog;
import com.rc.saas.tenant.vo.member.MemberResultVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.mapreduce.GroupBy;
import org.springframework.data.mongodb.core.mapreduce.GroupByResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


/**
 * Created by yang on 2022-05-26
 */
@Service
public class ChargeProcessLogServiceImpl implements ChargeProcessLogService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Autowired
    private MemberInfoMapper memberInfoMapper;

    @Autowired
    private ElecGunMapper elecGunMapper;

    @Autowired
    private ElecStationMapper elecStationMapper;

    @Autowired
    private ChargeProcessLogDao chargeProcessLogDao;

    @Override
    public PageImpl<ChargedProcessLog> findPageBySearch(ChargeProcessLogVo search) {
        OrderInfo orderInfo = null;

        if (StringUtils.isNotBlank(search.getOrderNum())) {
            orderInfo = orderInfoMapper.selectOrderByOrderNum(search.getOrderNum());
        } else if (StringUtils.isNotBlank(search.getGunNo()) && orderInfo != null) {
            ElecGun elecGun = elecGunMapper.selectByGunNo(search.getGunNo());
            orderInfo = orderInfoMapper.selectByElecGunId(elecGun.getElecGunId());
        } else if (StringUtils.isNotBlank(search.getMobile()) && orderInfo != null) {
            MemberInfo memberInfo = memberInfoMapper.selectBymobile(search.getNickName());
            orderInfo = orderInfoMapper.selectNewOrderByMemberId(memberInfo.getMemberId());
        } else if (StringUtils.isNotBlank(search.getOperatorName()) && orderInfo != null) {
            orderInfo = orderInfoMapper.selectByOperatorName(search.getOperatorName());
        }


        Query query = new Query();
        if (StringUtils.isNotBlank(search.getOrderNum())) {
            query.addCriteria(Criteria.where("orderNum").is(orderInfo.getOrderNum()));
            if (StringUtils.isNotBlank(search.getStartTime())) {
                query.addCriteria(Criteria.where("createTime").gte(search.getStartTime()));
            }
            if (StringUtils.isNotBlank(search.getEndTime())) {
                query.addCriteria(Criteria.where("createTime").lte(search.getEndTime()));
            }
        }

        Sort sort = null;
        if (search.getOrderBy() == 1) {
            sort = Sort.by(Sort.Direction.DESC, "createTime"); //倒序
        } else {
            sort = Sort.by(Sort.Direction.ASC, "createTime");
        }

        if (orderInfo != null) {
            //得到开始时间
            String startTime = DateHelper.dataToString(orderInfo.getStartTime(), DateHelper.DATE_FORMAT_YYYY_MM_DD);

            ChargedProcessLog chargeProcessLog = new ChargedProcessLog(startTime);

            PageImpl<ChargedProcessLog> pages = chargeProcessLogDao.findPage(query, sort, search.getOffset(), search.getLimit(), chargeProcessLog.getCollectionName());

            return pages;
        } else {
            return null;
        }
    }

    @Override
    public PageImpl<ChargedProcessLog> findOrderMonitorPageBySearch(ChargeProcessLogVo search) {
        //查找订单信息
        OrderInfoExample example = new OrderInfoExample();

        Query query = new Query();
        if (StringUtils.isNotBlank(search.getOrderNum())) {
            example.createCriteria().andOrderNumEqualTo(search.getOrderNum());

            query.addCriteria(Criteria.where("orderNum").is(search.getOrderNum()));
        } else if (StringUtils.isNotBlank(search.getOutOrderNum())) {
            example.createCriteria().andOutOrderNumEqualTo(search.getOutOrderNum());

            query.addCriteria(Criteria.where("outOrderNum").is(search.getOutOrderNum()));
        }

        Sort sort = null;
        if (search.getOrderBy() == 1) {
            sort = Sort.by(Sort.Direction.DESC, "createTime"); //倒序
        } else {
            sort = Sort.by(Sort.Direction.ASC, "createTime");
        }

        List<OrderInfo> list = orderInfoMapper.selectByExample(example);
        if (list != null && list.size() > 0) {

            //得到开始时间
            OrderInfo orderInfo = list.get(0);
            String startTime = DateHelper.dataToString(orderInfo.getStartTime(), DateHelper.DATE_FORMAT_YYYY_MM_DD);

            ChargedProcessLog chargeProcessLog = new ChargedProcessLog(startTime);

            PageImpl<ChargedProcessLog> pages = chargeProcessLogDao.findPage(query, sort, search.getOffset(), search.getLimit(), chargeProcessLog.getCollectionName());

            return pages;
        } else {
            return null;
        }
    }

    @Override
    public ChargedProcessLog findByOne(String id, Date startTime) {
        //得到开始时间
        String time = DateHelper.dataToString(startTime, DateHelper.DATE_FORMAT_YYYY_MM_DD);
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(id));
        ChargedProcessLog chargeProcessLog = new ChargedProcessLog(time);
        return chargeProcessLogDao.findOne(query, chargeProcessLog.getCollectionName());
    }


    public PageImpl<ChargedProcessLog> findPageBySearch1(ChargeProcessLogVo search) {
        //查找订单信息
        OrderInfoExample example = new OrderInfoExample();

        Query query = new Query();
        if (StringUtils.isNotBlank(search.getOrderNum())) {
            example.createCriteria().andOrderNumEqualTo(search.getOrderNum());

            query.addCriteria(Criteria.where("orderNum").is(search.getOrderNum()));
        } else if (StringUtils.isNotBlank(search.getOutOrderNum())) {
            example.createCriteria().andOutOrderNumEqualTo(search.getOutOrderNum());

            query.addCriteria(Criteria.where("outOrderNum").is(search.getOutOrderNum()));
        }
        Sort sort = Sort.by(Sort.Direction.ASC, "createTime");

        List<OrderInfo> list = orderInfoMapper.selectByExample(example);
        if (list != null && list.size() > 0) {

            //得到开始时间
            OrderInfo orderInfo = list.get(0);
            String startTime = DateHelper.dataToString(orderInfo.getStartTime(), DateHelper.DATE_FORMAT_YYYY_MM_DD);

            ChargedProcessLog chargeProcessLog = new ChargedProcessLog(startTime);

            Criteria criteria = Criteria.where("orderNum").is(search.getOrderNum());
            GroupBy groupBy = new GroupBy();
            GroupByResults<ChargedProcessLog> group = chargeProcessLogDao.group(criteria, chargeProcessLog.getCollectionName(), groupBy);

            PageImpl<ChargedProcessLog> pages = chargeProcessLogDao.findPage(query, sort, search.getOffset(), search.getLimit(), chargeProcessLog.getCollectionName());

            return pages;
        } else {
            return null;
        }
    }

}
